/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.domain.DoctorPicture;
import com.oyun.modules.medicine.domain.vo.DoctorPictureQueryCriteria;
import com.oyun.modules.medicine.mapper.DoctorPictureMapper;
import com.oyun.modules.medicine.service.DoctorPictureService;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author oyun
* @date 2024-04-29
**/
@Service
@RequiredArgsConstructor
public class DoctorPictureServiceImpl extends ServiceImpl<DoctorPictureMapper, DoctorPicture> implements DoctorPictureService {

    private final DoctorPictureMapper doctorPictureMapper;

    @Override
    public PageResult<DoctorPicture> queryAll(DoctorPictureQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(doctorPictureMapper.findAll(criteria, page));
    }

    @Override
    public List<DoctorPicture> queryAll(DoctorPictureQueryCriteria criteria){
        return doctorPictureMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DoctorPicture resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DoctorPicture resources) {
        DoctorPicture doctorPicture = getById(resources.getId());
        doctorPicture.copy(resources);
        saveOrUpdate(doctorPicture);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<DoctorPicture> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DoctorPicture doctorPicture : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" doctorId",  doctorPicture.getDoctorId());
            map.put(" pathImage",  doctorPicture.getPathImage());
            map.put(" createBy",  doctorPicture.getCreateBy());
            map.put(" createTime",  doctorPicture.getCreateTime());
            map.put(" updateBy",  doctorPicture.getUpdateBy());
            map.put(" updateTime",  doctorPicture.getUpdateTime());
            map.put(" remark",  doctorPicture.getRemark());
            map.put(" status",  doctorPicture.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
